﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyQuangCao
{
    public partial class frmDMKH_Edit : Form
    {
        public string strconn = @"Data Source=thonglh;Initial Catalog=QuanLyQuangCao;Integrated Security=True";
        SqlConnection sc = new SqlConnection();
        SqlCommand scmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        BindingSource bds = new BindingSource();

        private string maKH;

        public void setMaKH(string maKH)
        {
            this.maKH = maKH;
        }

        public enum myEdit
        {
            New = 1,
            Edit = 2,
        }

        public myEdit myNew_Edit;

        public void Load(myEdit myNew_Edit)
        {
            this.myNew_Edit = myNew_Edit;
        }

        public frmDMKH_Edit()
        {
            InitializeComponent();

            this.btDongY.Click += new EventHandler(btDongY_Click);
            this.btHuy.Click += new EventHandler(btHuy_Click);
        }

        public void btDongY_Click(object sender, EventArgs e)
        {
            if (myNew_Edit == myEdit.New)
            {
                string strMa_Khach = txtMa_Khach.Text;
                string strTen_Khach = txtTen_Khach.Text;
                string strSo_Dien_Thoai = txtSo_Dien_Thoai.Text;
                int iChiet_Khau = Convert.ToInt16(txtChiet_Khau.Text);

                string sql =
                    "INSERT KHACHHANG (Ma_Khach, Ten_Khach, So_Dien_Thoai, Chiet_Khau)" +
                    "VALUES ('" + strMa_Khach + "' , '" + strTen_Khach + "', '" + strSo_Dien_Thoai + "', '" + iChiet_Khau + "')";
                sc = new SqlConnection(strconn);

                scmd = new SqlCommand(sql, sc);

                sc.Open();
                scmd.ExecuteNonQuery();

                CapNhat();

                sc.Close();
                this.Close();
            }

            if (myNew_Edit == myEdit.Edit)
            {
                string strMa_Khach = txtMa_Khach.Text;
                sc = new SqlConnection(strconn);
                string str =    "UPDATE KHACHHANG SET Ma_Khach = '" + txtMa_Khach.Text +"', Ten_Khach = '" + txtTen_Khach.Text + "', " + 
                                "So_Dien_Thoai = '" + txtSo_Dien_Thoai.Text +"', Chiet_Khau = '" + txtChiet_Khau.Text + "'" +
                                "WHERE Ma_Khach = '" + maKH + "'";

                scmd = new SqlCommand(str, sc);
                sc.Open();
                scmd.ExecuteNonQuery();
                CapNhat();
                sc.Close();
                this.Close();
            }
            
            
        }

        public void btHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void CapNhat()
        {
            //Cap nhat lai du lieu cho frmDangKy_View
            string strKhachHang = @"SELECT * FROM KhachHang";
            frmDMKH_View.view.myconn.GetData(strKhachHang, strconn, ds, "KhachHang");
            da = new SqlDataAdapter(strKhachHang, strconn);
            dt = new DataTable();
            da.Fill(dt);
            bds.DataSource = dt;
            frmDMKH_View.view.bds.DataSource = dt;
            frmDMKH_View.view.Update(frmDMKH_View.view.bds);
            
        }

    }
}
